<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Mocha Tests</title>
    <link rel="stylesheet" href="vendor/mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <div id="fixture" style="position: absolute; top: -9999px; left: -9999px"></div>

    <!-- Test Runner -->
    <script src="./vendor/sinon.js"></script>
    <script src="./vendor/mocha.js"></script>
    <script src="./vendor/chai.js"></script>
    <script>
      mocha.setup('bdd');
      var assert = chai.assert;

      // Export sinon.assert methods onto assert.
      sinon.assert.expose(assert, {prefix: ''});

      var ckan = {ENV: 'testing'};
    </script>

    <!-- Source -->
    <script src="../vendor/jed.js"></script>
    <script src="../vendor/jquery.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.js"></script>
    <script src="../javascript/plugins/jquery.inherit.js"></script>
    <script src="../javascript/plugins/jquery.proxy-all.js"></script>
    <script src="../javascript/plugins/jquery.url-helpers.js"></script>
    <script src="../javascript/plugins/jquery.date-helpers.js"></script>
    <script src="../javascript/plugins/jquery.slug.js"></script>
    <script src="../javascript/plugins/jquery.slug-preview.js"></script>
    <script src="../javascript/plugins/jquery.form-warning.js"></script>
    <script src="../javascript/sandbox.js"></script>
    <script src="../javascript/module.js"></script>
    <script src="../javascript/pubsub.js"></script>
    <script src="../javascript/client.js"></script>
    <script src="../javascript/notify.js"></script>
    <script src="../javascript/i18n.js"></script>
    <script src="../javascript/main.js"></script>
    <script src="../javascript/view-filters.js"></script>
    <script src="../javascript/modules/basic-form.js"></script>
    <script src="../javascript/modules/autocomplete.js"></script>
    <script src="../javascript/modules/resource-form.js"></script>
    <script src="../javascript/modules/resource-upload-field.js"></script>
    <script src="../javascript/modules/image-upload.js"></script>
    <script src="../javascript/modules/confirm-action.js"></script>
    <script src="../javascript/modules/custom-fields.js"></script>
    <script src="../javascript/modules/followers-counter.js"></script>

    <!-- Suite -->
    <script src="./spec/ckan.spec.js"></script>
    <script src="./spec/i18n.spec.js"></script>
    <script src="./spec/module.spec.js"></script>
    <script src="./spec/sandbox.spec.js"></script>
    <script src="./spec/client.spec.js"></script>
    <script src="./spec/pubsub.spec.js"></script>
    <script src="./spec/notify.spec.js"></script>
    <script src="./spec/view-filters.spec.js"></script>
    <script src="./spec/modules/resource-form.spec.js"></script>
    <script src="./spec/modules/resource-upload-field.spec.js"></script>
    <script src="./spec/modules/image-upload.spec.js"></script>
    <script src="./spec/modules/confirm-action.spec.js"></script>
    <script src="./spec/modules/basic-form.spec.js"></script>
    <script src="./spec/modules/autocomplete.spec.js"></script>
    <script src="./spec/modules/custom-fields.spec.js"></script>
    <script src="./spec/modules/followers-counter.spec.js"></script>
    <script src="./spec/plugins/jquery.inherit.spec.js"></script>
    <script src="./spec/plugins/jquery.proxy-all.spec.js"></script>
    <script src="./spec/plugins/jquery.url-helpers.spec.js"></script>
    <script src="./spec/plugins/jquery.date-helpers.spec.js"></script>
    <script src="./spec/plugins/jquery.slug.spec.js"></script>
    <script src="./spec/plugins/jquery.slug-preview.spec.js"></script>
    <script src="./spec/plugins/jquery.form-warning.spec.js"></script>

    <script>
      /* Helper function for loading snippets from the ajax_snippets directory.
       * The filename should be provided and an optional object of query
       * string parameters. The returned snippet will be loaded into the
       * fixture and passed to any callback.
       *
       * The callback shares the same scope as the rest of the suite so you
       * can assign test variables easily. It is passed the loaded HTML string
       * and the fixture element.
       *
       * filename - The snippet filename to load.
       * params   - An optional object of query string params.
       * callback - An optional callback to call when loaded.
       *
       * Example:
       *
       *   // To simply load a fixture.
       *   beforeEach(function (done) {
       *     this.loadFixture('my-snippet', done);
       *   });
       *
       *   // To get the html itself.
       *   beforeEach(function (done) {
       *     this.loadFixture('my-snippet', function (html) {
       *       this.template = html;
       *       done();
       *     });
       *   });
       *
       */
      function loadFixture(filename, params, callback) {
        var context = this;

        // Allow function to be called without params.
        if (typeof params === 'function') {
          callback = params;
          params = {};
        }

        return  (new ckan.Client()).getTemplate(filename, params).fail(function () {
          throw new Error('Unable to load fixture: ' + filename);
        }).pipe(function (template) {
          var fixture = jQuery('#fixture').html(template);
          callback && callback.call(context, template, fixture);
          return template;
        });
      }

      before(function () {
        // Assign the loadFixture helper. Callbacks will have the same scope
        // as the before() functions.
        this.loadFixture = jQuery.proxy(loadFixture, this);
      });

      beforeEach(function () {
        this.fixture = jQuery('#fixture').empty();
      });

      afterEach(function () {
        this.fixture.empty();
      });


      // Add dummy translations for testing i18n
      ckan.i18n.load({
        '': {
          "domain": "ckan",
          "lang": "en",
          "plural_forms": "nplurals=2; plural=(n != 1);"
        },
        'foo': [null, 'FOO'],
        'hello %(name)s!': [null, 'HELLO %(name)s!'],
        'bar': ['bars', 'BAR', 'BARS'],
        '%(color)s shirt': [
          '%(color)s shirts',
          '%(color)s SHIRT',
          '%(color)s SHIRTS'
        ],
        '%(num)d item': ['%(num)d items', '%(num)d ITEM', '%(num)d ITEMS']
      });


      if (window.mochaPhantomJS) {
        mochaPhantomJS.run();
      } else {
        mocha.run();
      }

    </script>
  </body>
</html>
